package stardust.product.platform.webc.log;

import java.util.LinkedList;
import java.util.Queue;


public class AccessLog
{
  
  public static final int SNAPSHOT_QUEUE_LENGTH = 5;
  
  private long accessCount;
  private long lastAccessTime;

  private int errorCount;
  private long lastErrorTime;
  private Throwable lastError;
  
  private long consumptionTime;
  
  private Queue<RequestSnapshot> snapshotQueue = new LinkedList<RequestSnapshot>();
  
  public void incrementAccess()
  {
    this.accessCount++;
    this.lastAccessTime = System.currentTimeMillis();
  }
  
  public void errorOccurred(Throwable exception)
  {
    this.errorCount++;
    this.lastErrorTime = System.currentTimeMillis();
    this.lastError = exception;
  }
  
  public void incrementConsumptionTime(long consumptionTime)
  {
    this.consumptionTime += consumptionTime;
  }
  
  public void putSnapshot(RequestSnapshot snapshot)
  {
    if (this.snapshotQueue.size() + 1 > AccessLog.SNAPSHOT_QUEUE_LENGTH)
    {
      this.snapshotQueue.poll();
    }
    
    this.snapshotQueue.add(snapshot);
  }

  public long getAccessCount()
  {
    return accessCount;
  }

  public long getLastAccessTime()
  {
    return lastAccessTime;
  }

  public int getErrorCount()
  {
    return errorCount;
  }

  public long getLastErrorTime()
  {
    return lastErrorTime;
  }

  public Throwable getLastError()
  {
    return lastError;
  }
  
  public long getConsumptionTime()
  {
    return consumptionTime;
  }

  public Queue<RequestSnapshot> getSnapshotQueue()
  {
    return snapshotQueue;
  }
  
}
